﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WebFabricator.org
{
    public class WebConfiguration : ConfigurationSection
    {
        /// <summary>
        /// List of global adorners
        /// </summary>
        [ConfigurationProperty("",IsDefaultCollection=true)]
        [ConfigurationCollectionAttribute(typeof(AdornerElement), AddItemName = "adorner", RemoveItemName = "remove", ClearItemsName = "clear")]
        public AdornerElementCollection Adorners
        {
            get { return this[""] as AdornerElementCollection; }
            set { this[""] = value; } 
        }

        [ConfigurationProperty("valueproviders", IsDefaultCollection = false)]
        [ConfigurationCollectionAttribute(typeof(ValueProviderElement), AddItemName = "valueprovider", RemoveItemName = "remove", ClearItemsName = "clear")]
        public ValueProviderElementCollection ValueProviders
        {
            get { return this["valueproviders"] as ValueProviderElementCollection; }
            set { this["valueproviders"] = value; }
        }

        protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
        {
            return base.OnDeserializeUnrecognizedElement(elementName, reader);
        }

        /// <summary>
        /// Returns the tapestry.web configuration section
        /// </summary>
        /// <returns><TapestryConfigurationSection/returns>
        public static WebConfiguration GetSection()
        {
            return System.Configuration.ConfigurationManager.GetSection("webfabricator.org") as WebConfiguration;
        }
    }

    /// <summary>
    /// Defines an instance of an adorner in configuration
    /// </summary>
    public class AdornerElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name 
        {
            get { return this["name"] as string; }
            set { this["name"] = value; } 
        }

        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return this["type"] as string; }
            set { this["type"] = value; }
        }

    }

    /// <summary>
    /// A collection of AdornerElement(s) 
    /// </summary>
    public class AdornerElementCollection : ConfigurationElementCollection 
    {
        public override ConfigurationElementCollectionType  CollectionType 
        { 
            get
            { 
                return ConfigurationElementCollectionType.AddRemoveClearMap; 
            } 
        }

        public object[] Keys
        {
            get { return BaseGetAllKeys(); }
        }

        public AdornerElement this[int index] 
        {
            get { return (AdornerElement)BaseGet(index); } 
            set
            { 
                if (BaseGet(index) != null) 
                BaseRemoveAt(index); 
                BaseAdd(index, value); 
            } 
    
        }

        public AdornerElement this[object key]
        {
            get { return (AdornerElement)BaseGet(key); }
            set { base.BaseAdd(value);}

        }

        public void Add(AdornerElement element) 
        { 
            BaseAdd(element); 
        } 

        protected override ConfigurationElement CreateNewElement() 
        {
            return new AdornerElement(); 
        } 
 
        protected override object GetElementKey(ConfigurationElement element) 
        {
            return ((AdornerElement)element).Name; 
        }

        public void Remove(AdornerElement element) 
        { 
            BaseRemove(element.Name); 
        }  

        public void Remove(string name) 
        { 
            BaseRemove(name); 
        } 
   
        public void RemoveAt(int index) 
        { 
            BaseRemoveAt(index); 
        } 
    }

    /// <summary>
    /// Defines an instance of an adorner in configuration
    /// </summary>
    public class ValueProviderElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return this["name"] as string; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("type", IsRequired = true)]
        public string Type
        {
            get { return this["type"] as string; }
            set { this["type"] = value; }
        }

    }

    //// <summary>
    /// A collection of AdornerElement(s) 
    /// </summary>
    public class ValueProviderElementCollection : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        public object[] Keys
        {
            get { return BaseGetAllKeys(); }
        }

        public ValueProviderElement this[int index]
        {
            get { return (ValueProviderElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }

        }

        public ValueProviderElement this[object key]
        {
            get { return (ValueProviderElement)BaseGet(key); }
            set { base.BaseAdd(value); }

        }

        public void Add(ValueProviderElement element)
        {
            BaseAdd(element);
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ValueProviderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ValueProviderElement)element).Name;
        }

        public void Remove(ValueProviderElement element)
        {
            BaseRemove(element.Name);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

}
